<?php

/**
 * QuestionController
 * 
 * @author Radovan Dvorsky
 * @version 20100106
 */

require_once 'Zend/Controller/Action.php';

class QuestionController extends Zend_Controller_Action {
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$subjectModel = new Db_Subject();
		$cache = Zend_Registry::get('cache');
		$sid = $this->getRequest()->getParam('sid');		
		
		$questionsModel = new Db_Question();		
		
		if(!($questions = $cache->load('subjectWithQuestions' . $sid))){			
			$select = $subjectModel->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
			$select->setIntegrityCheck(false)			
			->join('question','question.sid=subject.sid')
			->where('question.sid = ?',$sid)
			->order('question.qn');
			$questions = $subjectModel->fetchAll($select);
			$cache->save($questions,'subjectWithQuestions' . $sid,array('subjectsWithQuestions'));
		}
		
		$subject = $subjectModel->find($sid)->current();
		
		$this->view->subject = $subject;		
		$this->view->questions = $questions;	
		
	}
	
	public function addAction(){
		$form = new Form_Question_Add();
		$this->view->form = $form;
		$sid = $this->getRequest()->getParam('sid');
		$this->view->sid = $sid;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				
				try{
				
					$questionModel = new Db_Question();	
					
					$questionsCount = $questionModel->getQuestionsCount($sid); //poradie otazky (celkovy pocet otazok + 1)
					
					/*
					 * Ak este nie je v databaze ziadna otazka tak vloz 0 (tj. prva v poradi)
					 * inak vloz pocet otazok + 1
					 */
					$ord = $questionsCount == 0 ? 0 : $questionsCount++;
					
					$data = array(
						'text' => $form->getValue('text'),
						'sid' => $sid,
						'qn' => $ord
					);
								
					$questionModel->insert($data);
					
					$cache = Zend_Registry::get('cache');
					$cache->remove('subjectWithQuestions' . $sid);
					$cache->remove('subjecsQuestionsCount');
					$cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,array('harmonograms'));
					
					$this->_redirect('/question/index/sid/' . $sid);
					
				} catch(Zend_Db_Exception $e){
					$this->view->message = 'Otázku sa nepodarilo pridať';
					
					$log = Zend_Registry::get('log');    		
	    			$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
				}				
			}
		}
	}
	
	public function editAction(){
		$form = new Form_Question_Add();
		$questionModel = new Db_Question();		
		$cache = Zend_Registry::get('cache');
		$qid = $this->getRequest()->getParam('qid');		
		
		if(!($question = $cache->load('question' . $qid))){			
			$questionRowset = $questionModel->find($qid);
			$question = $questionRowset->current();			
			$cache->save($question,'question' . $qid,array('questions'));
		}	
		
		$questionArray = $question->toArray();		
			
		$this->view->question = $questionArray;
		$this->view->form = $form->populate($questionArray);

		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){	
				
				try{
				
					$questionModel->update(array(
						'text' => $form->getValue('text')
						),
						$questionModel->getAdapter()->quoteInto('qid = ?',$qid,Zend_db::INT_TYPE)
					);
					
					$cache->remove('question' . $qid);
					$cache->remove('subjectWithQuestions' . $question->sid);
					$cache->remove('subjecsQuestionsCount');
					
					$this->_redirect('/question/index/sid/' . $question->sid);
				
				}
				catch(Zend_Db_Exception $e){
					$this->view->message = 'Otázku sa nepodarilo pridať';
					
					$log = Zend_Registry::get('log');    		
	    			$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
				}
			}
		}
	}
	
	public function deleteAction(){
		/*
		 * Vypnut view renderer
		 */
		$this->_helper->viewRenderer->setNoRender(true);
		
		/*
		 * Vypnut layout
		 */
		$this->_helper->layout()->disableLayout();
		
		/*
		 * Ziskaj hodnoty (json)
		 */
		$qidPost = $this->getRequest()->getParam('id');		
		$sid = $this->getRequest()->getParam('other');
		
		if(!empty($qidPost)){
			/*
			 * Vytvor pole
			 */
			$qids = explode(',',$qidPost);
			
			try{
				
				$questionModel = new Db_Question();
				$adapter = $questionModel->getAdapter();
				$adapter->beginTransaction();
				
				foreach($qids as $qid){
					$questionModel->delete($adapter->quoteInto('qid = ?',$qid,Zend_Db::INT_TYPE));
				}
				
				$questionModel->orderQuestions($sid);
				
				$cache = Zend_Registry::get('cache');
				
				$cache->remove('subjectWithQuestions' . $sid);
				$cache->remove('subjecsQuestionsCount');
				
				$adapter->commit();
				echo '<p class="success">Otázky odstránené</p>';
				
			} catch(Zend_Db_Exception $e){
				/*
				 * Ak chyba tak nerob nic
				 */
				$adapter->rollBack();
				echo '<p class="error">Otázky sa nepodarilo odstrániť</p>';
				
				/*
				 * Chybu zapis do logu
				 */
				$log = Zend_Registry::get('log');    		
	    		$log->log(get_class($e) . ':' . $e->getMessage(),Zend_Log::ERR);
			}
		}else {
			$this->_redirect('/subject/');
		}
	}

}